<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>月度收支详情</title>
    <style>
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .stats-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        .stats-table th, .stats-table td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        .stats-table th {
            background-color: #f5f5f5;
        }
        .stats-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .stats-table tr:hover {
            background-color: #f0f0f0;
        }
        .summary {
            margin: 20px 0;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            display: flex;
        }
        .summary p {
            margin: 0;
            padding: 10px;
            font-size: 16px;
        }
        .back-btn {
            display: inline-block;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .back-btn:hover {
            background-color: #0056b3;
        }
        .income {
            color: #ee6666;  /* 红色 */
        }
        .expense {
            color: #91cc75;  /* 绿色 */
        }
        .balance {
            transition: color 0.3s;
        }
        .balance.positive {
            color: #ee6666;  /* 正数显示红色 */
        }
        .balance.negative {
            color: #91cc75;  /* 负数显示绿色 */
        }
        .money-cell {
            font-weight: bold;
        }
        .money-cell.income {
            color: #ee6666;
        }
        .money-cell.expense {
            color: #91cc75;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="statistics.html" class="back-btn">返回统计页面</a>
        <div class="header">
            <h1 id="monthTitle">2024年1月收支详情</h1>
        </div>
        
        <h3>月度汇总</h3>
        <div class="summary">
            <p>总收入：<span id="totalIncome" class="income">0</span> 元</p>
            <p>总支出：<span id="totalExpense" class="expense">0</span> 元</p>
            <p>结余：<span id="balance" class="balance">0</span> 元</p>
        </div>
        <div class="details">
            <h3>收支明细</h3>
            <table class="stats-table">
                <thead>
                    <tr>
                        <th>日期</th>
                        <th>类型</th>
                        <th>子类型</th>
                        <th>金额（元）</th>
                        <th>备注</th>
                    </tr>
                </thead>
                <tbody id="detailsTable">
                </tbody>
            </table>
        </div>
    </div>

    <script>
        // 从URL获取年月参数
        const urlParams = new URLSearchParams(window.location.search);
        const year = urlParams.get('year');
        const month = urlParams.get('month');

        // 更新页面标题
        document.getElementById('monthTitle').textContent = `${year}年${month}月收支详情`;

        // 获取月度详细数据
        fetch(`/api/statistics/monthly?year=${year}&month=${month}`)
            .then(response => response.json())
            .then(result => {
                if (result.success) {
                    const data = result.data;
                    
                    // 更新汇总信息
                    document.getElementById('totalIncome').textContent = data.totalIncome.toFixed(2);
                    document.getElementById('totalExpense').textContent = data.totalExpense.toFixed(2);
                    
                    // 更新结余并设置颜色
                    const balance = data.totalIncome - data.totalExpense;
                    const balanceElement = document.getElementById('balance');
                    balanceElement.textContent = balance.toFixed(2);
                    balanceElement.className = 'balance ' + (balance >= 0 ? 'positive' : 'negative');

                    // 更新明细表格
                    const tbody = document.getElementById('detailsTable');
                    tbody.innerHTML = '';
                    
                    data.details.forEach(item => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                            <td>${item.date}</td>
                            <td>${item.type === 'income' ? '收入' : '支出'}</td>
                            <td>${item.sub_type}</td>
                            <td class="money-cell ${item.type}">${item.money.toFixed(2)}</td>
                            <td>${item.remark || '-'}</td>
                        `;
                        tbody.appendChild(row);
                    });
                } else {
                    alert('获取月度详情失败：' + result.message);
                }
            })
            .catch(error => {
                console.error('获取月度详情出错：', error);
                alert('获取月度详情出错，请查看控制台了解详情');
            });
    </script>
</body>
</html>